package com.example.userp.app.myapplication.RcycleView.DividerItem;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Describe:
 * Created by ysy on 2018/9/19.
 */
public class BaseDividerItemDecoration extends BaseDivider {
    private static String TAG = "BaseDividerItemDecoration";
    private final Paint mPaint;

    private BaseDividerItemDecoration() {
        mPaint = new Paint();
        mPaint.setColor(mDividerColor);
        mPaint.setStrokeWidth(mDividerStrokeWidth);
        mPaint.setAntiAlias(true);
    }


    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        RecyclerView.Adapter adapter = parent.getAdapter();
        int itemCount = adapter.getItemCount();
        int childAdapterPosition = parent.getChildAdapterPosition(view);

        if (childAdapterPosition == 0 && isShowFirstDivider) {//限制第一个
            outRect.top = mDividerStrokeWidth;
        }

        if (itemCount - 1 > childAdapterPosition) {//判断是否是最后一个
            outRect.bottom = mDividerStrokeWidth;
        } else {
            if (isShowLastDivider) {//限制最后一个
                outRect.bottom = mDividerStrokeWidth;
            }
        }
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        RecyclerView.Adapter adapter = parent.getAdapter();
        int itemCount = adapter.getItemCount();

        int childCount = parent.getChildCount();
        int height = parent.getHeight();
        int width = parent.getWidth();

        for (int i = 0; i < childCount; i++) {
            View childAt = parent.getChildAt(i);
            int childAdapterPosition = parent.getChildAdapterPosition(childAt);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) childAt.getLayoutParams();
            int left = parent.getPaddingLeft() + layoutParams.leftMargin + mLeftMargin;
            int top = childAt.getTop() - mDividerStrokeWidth;
            int right = width - parent.getPaddingRight() - layoutParams.rightMargin - mRightMargin;
            int bottom = childAt.getTop();
            if (top >= parent.getPaddingTop() - mDividerStrokeWidth && bottom <= height - parent.getPaddingBottom()) {
                if (childAdapterPosition > 0) {
                    c.drawRect(left, top, right, bottom, mPaint);
                } else {
                    if (isShowFirstDivider) {
                        c.drawRect(left, top, right, bottom, mPaint);
                    }
                }
                if (childAdapterPosition == itemCount - 1 && isShowLastDivider) {
                    c.drawRect(left, childAt.getBottom(), right, childAt.getBottom() + mDividerStrokeWidth, mPaint);
                }
            }
        }

    }


}
